﻿


function onPollData(r) {
    var cmd = r.responseText.split('|');
    // console.log( 'poll got: ', cmd );
    if (cmd.length > 0) {
        if (cmd[0] == 'instrument-created' ||
			cmd[0] == 'instrument-updated' ||
			cmd[0] == 'instrument-deleted') {
            updateInstrumentsList();
        }
    }
    setTimeout('pollOnce()', 1000);
}

function pollOnce() {
    var sid = getCurrentSessionID();
    new Ajax.Request('/api/call', {
        parameters: { 'poll': sid },
        onSuccess: onPollData
    });
}

function beginPolling() {
    setTimeout('pollOnce()', 1000);
}


function createNewInstrument() {
    var p = prompt('Please provide a name for your new instrument', '');
    if (p && p != '') {
        var ins = randomid('ins');
        var sid = getCurrentSessionID();
        new Ajax.Request('/api/call', {
            parameters: {
                'createinstrument': ins,
                'session': sid,
                'name': p
            },
            onSuccess: onUpdatedInstrument
        });
    }
}

function removeCurrentInstrument() {
    if (confirm('Are you sure you want to delete this instrument?') == 1) {
        var ins = getCurrentInstrumentID();
        var sid = getCurrentSessionID();
        new Ajax.Request('/api/call', {
            parameters: {
                'deleteinstrument': ins,
                'session': sid
            },
            onSuccess: onUpdatedInstrument
        });
    }
}

function onInstrumentList(r) {


    var el = $('instrumentdropdown');

    var oldsel = getDropdownValue(el);
    
    el.options.length = 0;

    var data = r.responseText.evalJSON(false);
    for (var j = 0; j < data.instruments.length; j++) {
        var item = data.instruments[j];
        el.options[el.options.length] = new Option(item.name, item.id);
    }

    el.selectedIndex = 0;

    setDropdownValue(el, oldsel);

    beginLoadInstrument();
}

function updateInstrumentsList() {
    new Ajax.Request('/api/call', {
        parameters: { 'getinstrumentlist': '1' },
        onSuccess: onInstrumentList
    });
}

function getCurrentInstrumentID() {
    var el2 = $('instrumentdropdown');
    var pat = el2.options[el2.selectedIndex].value;
    return pat;
}


function changeInstrument(event) {
    beginLoadInstrument();
}

var g_CurrentInstrument = null;

function onInstrumentData(r) {
    var data = r.responseText.evalJSON(false);
    console.log(data);

    g_CurrentInstrument = data;

    $('insname').value = g_CurrentInstrument.name;
    // $('instype').value = g_CurrentInstrument.type;
    $('inschannel').value = g_CurrentInstrument.midichannel;
    // $('insdevice').value = g_CurrentInstrument.mididevice;
    setDropdownValue('insdevice', g_CurrentInstrument.mididevice);
    $('inspatch').value = g_CurrentInstrument.midipatch;
    setDropdownValue('insdefaulteditorui', g_CurrentInstrument.defaulteditorui);
    setDropdownValue('insdefaultdeviceui', g_CurrentInstrument.defaultdeviceui);

}

function beginLoadInstrument() {
    var ins = getCurrentInstrumentID();
    new Ajax.Request('/api/call', {
        parameters: { 'getinstrument': ins },
        onSuccess: onInstrumentData
    });

}

function propertyChanged(event) {
    saveInstrument();
}

function onUpdatedInstrument(r) {
    updateInstrumentsList();
}

function saveInstrument() {
    var ins = getCurrentInstrumentID();
    var sid = getCurrentSessionID();
    new Ajax.Request('/api/call', {
        parameters: {
            'updateinstrument': ins,
            'session': sid,
            'name': $('insname').value,
            // 'type': $('instype').value,
            'mididevice': getDropdownValue('insdevice'),
            'midichannel': $('inschannel').value,
            'midipatch': $('inspatch').value,
            'defaultdeviceui': getDropdownValue('insdefaultdeviceui'),
            'defaulteditorui': getDropdownValue('insdefaulteditorui')
        },
        onSuccess: onUpdatedInstrument
    });
}







var g_DeviceList = [];




function initInstrumentsView() {
    initPageHeader('INS'); 

    var el = $('insdefaultdeviceui');
    el.options.length = 0;
    el.options[el.options.length] = new Option('- None -', '');
    for (var j = 0; j < g_DeviceUIs.length; j++) {
        el.options[el.options.length] = new Option(g_DeviceUIs[j].name, g_DeviceUIs[j].id);
    }

    el = $('insdefaulteditorui');
    el.options.length = 0;
    el.options[el.options.length] = new Option('- None -', '');
    for (var j = 0; j < g_EditorUIs.length; j++) {
        el.options[el.options.length] = new Option(g_EditorUIs[j].name, g_EditorUIs[j].id);
    }

    el = $('insdevice');
    el.options.length = 0;
    el.options[el.options.length] = new Option('- None -', '-1');
    for (var j = 0; j < g_MidiDevices.length; j++) {
        el.options[el.options.length] = new Option(g_MidiDevices[j].name, g_MidiDevices[j].id);
    }

    updateInstrumentsList();
    beginPolling();
}
